<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<script type="text/javascript" src="${ctx}/js/jquery.itextclear.js"></script>
<script type="text/javascript" src="${ctx}/js/global_util.js"></script>
<script type="text/javascript">
	$(document)
			.ready(
					function() {
						$(
								'input[type=text], input[type=password], input[type=url], input[type=email], input[type=number], textarea',
								'.form').iTextClear();
					});
</script>
<!-- jQplot CSS -->

<link rel="stylesheet" media="screen" href="${ctx}/CSS/Tree.css" />
<!-- jQplot CSS END -->

<!-- DATATABLES CSS -->
<link rel="stylesheet" media="screen"
	href="${ctx}/lib/datatables/css/vpad.css" />
<script type="text/javascript">
	$(document).ready(function() {
		$('#example').dataTable({
			"sPaginationType" : "full_numbers"
		});
	});
</script>
<!-- DATATABLES CSS END -->
<div class="container_8 clearfix leading">
	<section class="tabs grid_8 leading">
		<article>
			周期筛选：
			<div class="selector" id="uniform-form-timezone">
				<span style="-moz-user-select: none;">月</span><select
					style="opacity: 0;" id="sel_periodtype" onchange="selectChangeListener(this)">
					<option value="monthly">月</option>
					<option value="bimonthly">双月</option>
					<option value="season">季度</option>
					<option value="semiyearly">半年</option>
					<option value="annually">年</option>
				</select>
			</div>
			<p />
		</article>
		<ul class="clearfix" id="tab_type">
			<li id="li_percentage"t><a href="javascript:void(0)"
				<s:if test="#request.examQualDistSet.type.toString().equals('percentage')">class="current" </s:if>
				>百分比制
					<input id="percentage" name="radio_dist" type="radio" style="padding-left: 20px"
					value="percentage"
					<s:if test="#request.examQualDistSet.type.toString().equals('percentage')">checked="checked" </s:if>
					onclick="handleDistSet(${organizationId},'percentage','${listAction}',this)" />
			</a></li>
			<li id="li_custom_field"><a href="javascript:void(0)"
				<s:if test="#request.examQualDistSet.type.toString().equals('custom_field')">class="current" </s:if>
				>自定人数制
					<input id="custom_field" name="radio_dist" type="radio" style="padding-left: 20px"
					value="custom_field"
					<s:if test="#request.examQualDistSet.type.toString().equals('custom_field')">checked="checked" </s:if>
					onclick="handleDistSet(${organizationId},'custom_field','${listAction}',this)" />
			</a></li>
			<li id="li_no_distribution"><a href="javascript:void(0)"
				<s:if test="#request.examQualDistSet.type.toString().equals('no_distribution')">class="current" </s:if>
				>无强制性分布
					<input id="no_distribution" name="radio_dist" type="radio" style="padding-left: 20px"
					value="no_distribution"
					<s:if test="#request.examQualDistSet.type.toString().equals('no_distribution')">checked="checked" </s:if>
					onclick="handleDistSet(${organizationId},'no_distribution','${listAction}',this)" />
			</a></li>
		</ul>
		<section>
			<section class="clearfix" style="display: block;">
				<header class="grid_12 alpha omega"></header>
				<div id="list_content_percentage" class="clearfix"></div>
				<!-- 控制层 -->

				<div class="tabs side grid_12 leading">
					<div class="clear"></div>
					<div class="grid_12">
						<div class="form-action clearfix">
							<p>
								<input type="checkbox" value="1" id="form-updates-i1"
									onchange="checkAll(this,'chk_qualset')" /><label
									for="form-updates-i1">全选</label> <a class="button"
									href="javascript:void(0)"
									onclick="deleteExamqualSets('percentage')">批量删除</a> <a
									class="button" href="javascript:void(0)"
									onclick="dispatchAddSet(${organizationId},'percentage')"><span
									style="background: url(${ctx}/images/img/add.png)" class="icon">&nbsp;</span>添加</a>
							</p>
						</div>
					</div>
				</div>
			</section>
			<section class="clearfix" style="display: none;">
				<header class="grid_12 alpha omega"></header>
				<div id="list_content_custom_field" class="clearfix"></div>
				<!-- 控制层 -->
				<div class="tabs side grid_12 leading">
					<div class="clear"></div>
					<div class="grid_12">
						<div class="form-action clearfix">
							<p>
								<input type="checkbox" value="1" id="form-updates-i2"
									onchange="checkAll(this,'chk_qualset')" /><label
									for="form-updates-i2">全选</label> <a class="button"
									href="javascript:void(0)"
									onclick="deleteExamqualSets('custom_field')">批量删除</a> <a
									class="button"
									href="javascript:void(0)" onclick="dispatchAddSet(${organizationId},'custom_field')"><span
									style="background: url(${ctx}/images/img/add.png)" class="icon">&nbsp;</span>添加</a>
							</p>
						</div>
					</div>
				</div>
			</section>
			<section class="clearfix" style="display: none;"
				id="list_content_no_distribution">
				<header class="grid_12 alpha omega">
					<h2>已设定为不实行强制性分布</h2>
				</header>
			</section>
		</section>
	</section>
</div>

<script type="text/javascript">
	var organizationId = ${organizationId};
	var curdist_type = "";
	
	$(document).ready(function() {
		
		curdist_type = "${examQualDistSet.type}";
		loadListContent('/exam/qualset_list.xhtml', '${examQualDistSet.type}');
	});
	
	function dispatchAddSet(orgId,dtype){
		var periodType = $("#sel_periodtype").val();
		if(dtype == "percentage"){
			$.ajax({
	            type:"post",
	            url:"/ajax/exam_qual_validateinduce.xhtml?organizationId=" + orgId + "&periodEnum=" + periodType,
	            //data: "name=John&location=Boston",
	            dataType:"json",
	            success:function (data) {
	            	var resultObj = eval(data);
	            	if(resultObj.checkseted == false){
	            		alert("没有对组织上级设置分布，请先设置上级组织分布");
	            		return;
	            	}
	                if(resultObj.percentage >= 1){
	                    alert("强制分布已分配完100%");
	                    return;
	                }
	                window.location.href = "#exam/qualset_add.xhtml?organizationId=" + orgId + "&dispatchType=" + dtype + "&periodType=" + periodType + "&t=<%=new java.util.Date().getTime()%>";
	            }
	        });
		}else{
			window.location.href = "#exam/qualset_add.xhtml?organizationId=" + orgId + "&dispatchType=" + dtype + "&periodType=" + periodType + "&t=<%=new java.util.Date().getTime()%>";
		}
	}
	
	function handleDistSet(orgId,type,url,evtObj){
		var periodType = $("#sel_periodtype").val();	
		if(confirm("警告！选择后之前对该部门的分布设定全部清出,你确定要这么操作么？")){
			var data = {
					"organizationId" : orgId,
					"type" : type,
					"periodType" : periodType
				};
			var url = "${ctx}/exam/post_qualdistset.xhtml";
			$.ajax(url, {
			    type:"POST",
			    data:data,
			    dataType:"json",
			    cache:false,
			    success:function(a) {
			    	$("#tab_type li a").removeAttr("class");
					$("#tab_type li").each(function(i){
						if(this.id == "li_" + type){
							$(this).find("a").addClass("current");
						}
					});
			        //alert('设定成功.');
			        loadListContent('/exam/qualset_list.xhtml', type,evtObj);
			    },
			    error:function (XMLHttpRequest, textStatus, errorThrown) {
	                alert('提交申请发生异常,请联系管理人员.');
	            },
			    complete:function(b) {
			    }
			});
			curdist_type = type;
		}else{
			//class="current"
			$("#tab_type li a").removeAttr("class");
			$("#tab_type li input").removeAttr("checked");
			$("#tab_type li").each(function(i){
				if(this.id == "li_" + curdist_type){
					$(this).find("a").addClass("current");
					$("#" + curdist_type).attr("checked","checked");
				}
			});
		}
	}
	
	/** 改变监听 */
	function selectChangeListener(evtObj){
		$(evtObj).prev().html($(evtObj).find("option:selected").text());
		//读取type
		var data = {
			"organizationId" : organizationId,
			"periodEnum" : evtObj.value
		};
		var url = "${ctx}/ajax/exam_qual_checkdisttype.xhtml";
		$.ajax(url, {
			type : "POST",
			data : data,
			dataType : "html",
			cache : false,
			success : function(a) {
				var result = eval("(" + a + ")");
				$("#tab_type a").removeClass();
				$("#tab_type input[type='radio']").removeAttr("checked");
				if(result.type == "percentage"){
					$("#li_percentage").find("a").attr("class","current");
					$("#li_percentage").find("input").attr("checked","checked");
				}else if(result.type == "custom_field"){
					$("#li_custom_field").find("a").attr("class","current");
					$("#li_custom_field").find("input").attr("checked","checked");
				}else{
					$("#li_no_distribution").find("a").attr("class","current");
					$("#li_no_distribution").find("input").attr("checked","checked");
				}
				loadListContent('/exam/qualset_list.xhtml', result.type);
			},
			complete : function() {
			}
		});
	}

	function loadListContent(url, type,evtObj) {
		var periodtype = $("#sel_periodtype").val();
		if(evtObj != null){
			$(evtObj).parent().parent().find("a").removeClass("current");
			$(evtObj).parent().addClass("current");
		}
		if(type == "no_distribution"){
			$("#list_content_" + type).parent().find("section").attr("style","display:none");
			$("#list_content_" + type).attr("style","display:block");
			return;
		}
		var data = {
			"organizationId" : organizationId,
			"type" : type,
			"periodType" : periodtype
		};
		$.ajax(url, {
			type : "POST",
			data : data,
			dataType : "html",
			cache : false,
			success : function(a) {
				var obj = $("#list_content_" + type);
				$("#list_content_" + type).parent().parent().find("section").attr("style","display:none");
				obj.parent().attr("style","display:block");
				obj.html("");
				obj.append(a);
			},
			complete : function() {
			}
		});
	}
	/**
	 * 删除安排设置
	 */
	function deleteExamqualSets(type) {
		var checkIds = new Array();
		$("input[type='checkbox'][name='chk_qualset']").each(function() {
			if (this.checked) {
				checkIds.push(this.value);
			}
		});
		if (checkIds.length == 0) {
			return;
		}
		if (confirm("确定要删除选中的数据吗？")) {
			doDeleteDatas("${ctx}/exam/qualset_delete.xhtml", checkIds
					.join(","),type , type == "percentage" ? $("#sel_percentype").val():$("#sel_customtype").val());
		}
	}
	
	/**
    * 删除指定数据集
    * @param url
    * @param ids
    */
	function doDeleteDatas(url,ids,type,periodType) {
		$.ajax({
			type : "get",
			url : url,
			data: {"ids":ids},
			dataType : "json",
			success : function(data) {
				// load the grade list
				//currentTabOrgId
				if(typeof(currentTabOrgId) == 'number'){
					loadListContent("${ctx}${listAction}", type, periodType);
				}else{
					loadListContent("${ctx}${listAction}", type, periodType);
				}
			}
		});
	}
</script>